﻿Friend Class InputKeyCodeForm

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub InputKeyCodeForm_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
        If e.KeyData = Keys.Escape Then Me.Close()
    End Sub

    Private Sub ComboBox1_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox1.DropDownClosed
        Button1.Focus()
    End Sub

    Friend Function DXKeyCode(ByVal strCode As String, ByRef code As Integer) As Boolean
        Dim frm = New InputKeyCodeForm()

        For Each item As String In frm.ComboBox1.Items
            If item.StartsWith(strCode) Then
                frm.ComboBox1.Text = item
                Exit For
            End If
        Next

        If String.IsNullOrEmpty(frm.ComboBox1.Text) Then
            frm.ComboBox1.Text = strCode
        End If

        If frm.ShowDialog(MainForm) <> Windows.Forms.DialogResult.OK Then Return False

        If Integer.TryParse(frm.ComboBox1.Text.Trim(), code) = False Then
            Dim n = frm.ComboBox1.Text.IndexOf(" "c)
            If n = -1 Then Return False

            Dim str = frm.ComboBox1.Text.Remove(n).TrimStart
            If Integer.TryParse(str, code) = False Then Return False

        End If
        Return True
    End Function

End Class